package com.mths.client.page;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mths.client.component.Button;
import com.mths.client.component.TextField;
import com.mths.client.constant.DirectorType;
import com.mths.client.dto.DirectorDTO;

public class DirectorRegister extends AbstractPage implements IPage {

	private static DirectorRegisterUiBinder	uiBinder	= GWT.create(DirectorRegisterUiBinder.class);

	interface DirectorRegisterUiBinder extends UiBinder<Widget, DirectorRegister> {
	}

	@UiField
	TextField				mersisNo, tradeName, companyCenter, name, surname, title;

	@UiField
	Button					addManagerBtn;

	@UiField
	FlowPanel				corpDirectorPanel, realDirectorPanel;

	private String			directorType;

	private DirectorDTO		directorDTO	= new DirectorDTO();

	private CompanyRegister	companyRegister;

	public DirectorRegister() {
		initWidget(uiBinder.createAndBindUi(this));
		createEventHandler();
	}

	public void changePageDirectorType(String directorValue) {
		this.directorType = directorValue;
		if (directorType != null && directorType.equals(DirectorType.REAL.toString())) {
			corpDirectorPanel.setVisible(false);
			realDirectorPanel.setVisible(true);
		} else {
			corpDirectorPanel.setVisible(true);
			realDirectorPanel.setVisible(false);
		}

	}

	private void createEventHandler() {
		addManagerBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (DirectorType.CORPORATE.toString().equals(directorType)) {
					directorDTO.setCompanyCenter(companyCenter.getFieldValue());
					directorDTO.setMersisNo(mersisNo.getFieldValue());
					directorDTO.setTradeName(tradeName.getFieldValue());
				} else {
					directorDTO.setTitle(title.getFieldValue());
				}
				directorDTO.setName(name.getFieldValue());
				directorDTO.setSurname(surname.getFieldValue());
				directorDTO.setDirectorType(directorType);

				companyRegister.saveOrUpdateDirector(directorDTO);
			}
		});
	}

	public void openForUpdate(DirectorDTO directorDTO) {
		this.directorDTO = directorDTO;
		if (DirectorType.CORPORATE.toString().equals(directorType)) {
			companyCenter.setFieldValue(directorDTO.getCompanyCenter());
			mersisNo.setFieldValue(directorDTO.getMersisNo());
			tradeName.setFieldValue(directorDTO.getTradeName());
		} else {
			title.setFieldValue(directorDTO.getTitle());
		}
		name.setFieldValue(directorDTO.getName());
		surname.setFieldValue(directorDTO.getSurname());
		directorType = directorDTO.getDirectorType();
		changePageDirectorType(directorType);
	}

	public CompanyRegister getCompanyRegister() {
		return companyRegister;
	}

	public void setCompanyRegister(CompanyRegister companyRegister) {
		this.companyRegister = companyRegister;
	}

	public String getDirectorType() {
		return directorType;
	}

	public void setDirectorType(String directorType) {
		this.directorType = directorType;
	}

	@Override
	public String pageName() {
		return messages.DirectorRegister_pageName();
	}
}
